#include "yin.h"

YIN_Playback_t yin_playback = {0};

// 使用简单函数名
void YIN_Speech(const uint8_t *gb_data)
{
    if(yin_playback.is_playing) return;
    
    uint16_t len = strlen((const char*)gb_data);
    uint8_t ecc = 0;
    
    yin_playback.data[0] = 0xFD;
    yin_playback.data[1] = 0x00;
    yin_playback.data[2] = len + 3;
    yin_playback.data[3] = 0x01;
    yin_playback.data[4] = 0x01;

    for(int i = 0; i < 5; i++) {
        ecc ^= yin_playback.data[i];
    }
    for(int i = 0; i < len; i++) {
        ecc ^= gb_data[i];
    }

    memcpy(&yin_playback.data[5], gb_data, len);
    yin_playback.data[5 + len] = ecc;
    
    yin_playback.length = 5 + len + 1;
    yin_playback.index = 0;
    yin_playback.is_playing = 1;
    
    HAL_UART_Transmit_IT(&huart1, &yin_playback.data[0], 1);
}

uint8_t YIN_IsPlaying(void)
{
    return yin_playback.is_playing;
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart == &huart1 && yin_playback.is_playing) {
        yin_playback.index++;
        if(yin_playback.index < yin_playback.length) {
            HAL_UART_Transmit_IT(&huart1, &yin_playback.data[yin_playback.index], 1);
        } else {
            yin_playback.is_playing = 0;
        }
    }
}

